(function(){
    var widgetUtil = {};
    var cache = {};
    
    function initWidgetDiv(level){
        level = level ? level : 0;
        var levelDivId = "widget_l"+level;
        if($("#widget_panel").length==0){
            $("body").append("<div id='widget_panel'></div>");
        }
        var p = $("#widget_panel");
        if($("#"+levelDivId).length==0){
            p.append("<div id='"+levelDivId+"'></div>");
        }
        $("#"+levelDivId).show();
        return $("#"+levelDivId);
    }
    function hideWidget(level){
        level = level ? level : 0;
        var levelDivId = "widget_l"+level;
        if($("#widget_panel").length!=0&&$("#"+levelDivId).length!=0){
            $("#"+levelDivId).hide();
        }
    }
    function toast(e,showTime,thendo) {
        var show = showTime&&!isNaN(showTime) ? showTime : 2000;
        var toastHtml = 
        "<div id='toast'>" +
        "   <div id='toastBox'>" +
        "        <div id='toastText'></div>" +
        "    </div>" +
        "</div>";
        $toast = $(toastHtml);
        $toast.find("#toastText").html(e);
        initWidgetDiv().html($toast);
        setTimeout(function(){
            hideWidget();
            if(thendo){
            	thendo();
            }
        }, show);
    }
    function loadding(e) {
        var loaddingContent =
        '    <div class="loaddingBox">'+
        '        <div class="loaddingIcon">'+
        '            <span class="loaddingSpot loaddingSpot1"></span>'+
        '            <span class="loaddingSpot loaddingSpot2"></span>'+
        '            <span class="loaddingSpot loaddingSpot3"></span>'+
        '            <span class="loaddingSpot loaddingSpot4"></span>'+
        '            <span class="loaddingSpot loaddingSpot5"></span>'+
        '            <span class="loaddingSpot loaddingSpot6"></span>'+
        '            <span class="loaddingSpot loaddingSpot7"></span>'+
        '            <span class="loaddingSpot loaddingSpot8"></span>'+
        '            <span class="loaddingSpot loaddingSpot9"></span>'+
        '            <span class="loaddingSpot loaddingSpot10"></span>'+
        '            <span class="loaddingSpot loaddingSpot11"></span>'+
        '            <span class="loaddingSpot loaddingSpot12"></span>'+
        '        </div>'+
        '        <div id="loaddingText"></div>'+
        '    </div>';
        $loadding = $(loaddingContent);
        $loadding.find("#loaddingText").html(e);
        initWidgetDiv().html($loadding);
    }
    /** 提示语，按钮名，按钮事件，已有内容，字数限制，*/
    function showDialog(placeholder, btnText, subitmEvent, content, countLimit) {
        placeholder = defaultStr(placeholder,"请输入内容..."),
        btnText = defaultStr(btnText,"确认"),
        content = defaultStr(content,""),
        countLimit = !countLimit||countLimit==''?250:countLimit;
        subitmEvent = !subitmEvent ? function(){} : subitmEvent;
        var dialogHtml = '  <div class="dialog_box">'+
        '    <div class="dialog_textBox">'+
        '      <textarea id="dialog_content" rows="4" placeholder="'+placeholder+'" maxlength="'+countLimit+'"></textarea>'+
        '      <div class="dialog_promptNum"><span id="dialog_inputedNum">0</span>/'+countLimit+'</div>'+
        '    </div>'+
        '    <div id="dialog_cancel">取消</div>'+
        '    <div id="dialog_confirm">'+btnText+'</div>'+
        '  </div>';
        $showDialog=$(dialogHtml);
        initWidgetDiv(4).html($showDialog);
        $showDialog.find("#dialog_cancel").one("click",function(){
            hideWidget(4);
        });
        if(content){
            $("#dialog_content").val(content);
        }
        $("#dialog_content").bind('propertychange input', function () {  
            var counter = $('#dialog_content').val().length;
            $('#dialog_inputedNum').text(counter);
            /**有时间加个提示*/
        });
        $showDialog.find("#dialog_confirm").click(function(){
            var content = $('#dialog_content').val();
            subitmEvent(content);
        });
        $("#dialog_content").focus();
    }
    
    /**title,funcs */
    function flist(fobj,count,titleText){
        var btnHtmls = "";
        for(var i=0;i<count;i++) {
            btnHtmls+='<div class="functions_item" id="functions_item'+i+'" data-idx="'+i+'">'+fobj.title(i) +'</div>';
        }
        var html = 
        '    <div id="functions_box">'+
        '        <div class="functions_title" id="functions_title">'+titleText+'</div>'
        + btnHtmls +
        '        <div class="functions_item mt10 functions_cancel">取消</div>'+
        '    </div>';
        $flist = $(html);
        for(var i=0;i<count;i++) {
            $flist.find("#functions_item"+i).click(function(event){
                event.stopPropagation(); 
                hideWidget(5);
                fobj.func($(this).data("idx"));
            });
        }
        initWidgetDiv(5).one("click",function () {
            hideWidget(5);
        });
        $flist.on("click",function(event){
           event.stopPropagation(); 
        });
        $flist.find(".functions_cancel").one("click",function(){
           hideWidget(5);
        });
        initWidgetDiv(5).html($flist);
        if(!titleText){
            $("#functions_title").hide();
        };
    }
    function functionList(btnTitles,btnFuncs,titleText){
        if(btnTitles.length!=btnFuncs.length) {
            console.log("参数输入错误");
            return;
        }
        flist({
            title:function(idx){
                return btnTitles[idx];
            },
            func:function(idx){
                btnFuncs[idx]();
            }
        },btnTitles.length,titleText);
    }
    function functionObjs(funObjs,titleText){
        flist({
            title:function(idx){
                return funObjs[idx].title;
            },
            func:function(idx){
                funObjs[idx].func();
            }
        },funObjs.length,titleText);
    }
    function function2Titles(titles, fun, titleText){
        flist({
            title:function(idx){
                return titles[idx];
            },
            func:function(idx){
                fun(idx);
            }
        },titles.length,titleText);
    }
	
	
    function loaddingBar(e){
        var loaddingBarContent =
        '<div class="loaddingBar tc">'+
        '    <span class="color999">'+e+'</span>'+
        '    <span class="loaddingPotin1">.</span>'+
        '    <span class="loaddingPotin2">.</span>'+
        '    <span class="loaddingPotin3">.</span>'+
        '</div>';
        $loaddingBar = $(loaddingBarContent);
        $("#loadding").html($loaddingBar);
    }
    function hideLoadingBar(){
        if($("#loadding")){
            $("#loadding").html("");
        }
    }
    
    widgetUtil.loaddingBar = loaddingBar;
    widgetUtil.hideLoadingBar = hideLoadingBar;
    widgetUtil.hideWidget = hideWidget;
    /**0**/
    widgetUtil.toast = toast;
    widgetUtil.loadding = loadding;
    /**5[自动收起]**/
    widgetUtil.functionList = functionList;
    widgetUtil.functionObjs = functionObjs;
    widgetUtil.function2Titles = function2Titles;
    window.widgetUtil = widgetUtil;
    window.$w = widgetUtil;
})();

/**--------SwiperUtil-------**/
(function(){
    var swiperUtil = {};
    var item = {
        image:"",
        toUrl:""
    }
    function showSwiper(containId,images,types,ids,speed) {
        	var myspeed = speed && speed>0 ? speed : 3000;
        	var container = $(containId);
        var banners = "";
        for(var i=0;i<images.length;i++){
            var type = types&&types.length>i?types[i]:"";
            var id = ids&&ids.length>i?ids[i]:"";
            banners+='<div class="swiper-slide" data-type="'+type+'" data-id="'+id+'" style="background-image:url('+images[i]+')"></div>';
        }
        banners = '<div class="swiper-container">'+
                  '  <div class="swiper-wrapper">'+
                  banners+
                  '  </div>'+
                  '</div>'+
                  '<div class="swiper-pagination"></div>';
        container.html(banners);
        $(".swiper-slide").click(function(){
            var type = $(this).data("type");
            var id = $(this).data("id");
            bannerLink(type,id)
        });
        
        var mySwiper = new Swiper(containId + ' .swiper-container',{
            loop: true,
            autoplay: myspeed,
            pagination: containId + ' .swiper-pagination',
        });
    }
    
    swiperUtil.showSwiper = showSwiper;
    /**需要设置.swiper-pagination  .swiper-slide{ padding-bottom:60%; }等样式**/
    window.swiperUtil = swiperUtil;
})();
/**--------Pager-------**/
(function(){
    function pushArray(target,subarray) {
        for(var i=0;i<subarray.length;i++){
            target.push(subarray[i]);
        }
    }
    /**url必须包含PAGER_PAGE*/
    function Pager(URL,callback, preCall, processor,methon){
        this.page = 0;
        this.hasMore = true;
        this.loadding = false;
        this.count = 0;
        this.dataArr = [];
        
        this.queryPrefix = URL;
        this.queryer = function(){
            if(!this.hasMore||this.loadding) {
                return;
            }
            var pager = this;
            RPC.invokeApi(methon?methon:"GET",this.queryPrefix.replace("PAGER_PAGE",this.page),null, function() {
                pager.loadding=true;
                preCall();
            },function(n){
                pager.loadding=false;
                if(n.result.length>0) {
                    if(processor){processor(n.result);}
                    pushArray( pager.dataArr, n.result);
                    pager.page=pager.page+1;
                    pager.count = pager.dataArr.length;
                } else {
                    pager.hasMore=false;
                }
                callback(true, n.result);
            },function(n) {
                pager.loadding=false;
                callback(false, n);
            })
        };
        this.reset = function(){
            this.page = 0;
            this.hasMore = true;
            this.loadding = false;
            this.count = 0;
            this.dataArr = [];
        }
        this.canLoad=function(){
            return !(this.loadding||!this.hasMore);
        }
    }
    window.Pager = Pager;
})();
/**--------pull up-------**/
(function(){
    function onPullUp(onFun,jqItem,dis){
        var mainItem = jqItem ? jqItem : $("body>div");
        var pdis = dis ? dis : 800;
        $(window).scroll(function (event) {
            var loadheight = mainItem.height();
            var st = $(window).scrollTop();
            var hook=loadheight-st;
            console.log(hook);
            if(hook<pdis){
                onFun();
            }
        })
    }
    window.onPullUp = onPullUp;
})();
/**倒计时*/
(function(){
    var countTime,currentTime;
    var tt;
    var callback = function(currentTime) {

    };
    function init(time, back) {
        countTime = time ? time : 60;
        currentTime = countTime;
        if(back)callback=back;
        return countUtil;
    }
    function countDown(){
        if(currentTime<0) {
            console.log(currentTime+"小于0");
            currentTime=0;
            callback(currentTime);
            return;
        }
        currentTime --;
        console.log(currentTime+"/"+countTime);
        callback(currentTime);
    }
    function begin(){
        callback(currentTime);
        if(currentTime!=countTime) {
            console.log(currentTime+"/"+countTime);
            return;
        }
        tt = setInterval(countDown,1000);
        setTimeout(function(){
            clearInterval(tt);
        }, (countTime+2)*1000);
    }
    var countUtil = {};
    countUtil.init = init;
    countUtil.begin = begin;
    window.countUtil=countUtil;
})();